<template>
  <div class="login-container">
    <div class="form">
      <div id="login_form" v-if="page">
        <el-form ref="formRef" :model="form" class="login-form" :rules="rules">
          <div style="display: flex">
            <img src="@/assets/logo.png" alt="Local Image" class="img" />
            <div class="title-container">
              <h2 class="title">欢迎登陆超市进销管理系统</h2>
            </div>
          </div>
          <el-form-item prop="account">
            <svg-icon icon="user" class="svg-container"></svg-icon>
            <el-input v-model="loginForm.account"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <svg-icon icon="password" class="svg-container"></svg-icon>
            <el-input
              v-model="loginForm.password"
              :type="passwordType"
            ></el-input>
            <svg-icon
              :icon="passwordType === 'password' ? 'eye' : 'eye-open'"
              @click="changeType"
            ></svg-icon>
          </el-form-item>
          <div class="code">
            <el-form-item prop="code" style="width: 70%">
              <el-input v-model="loginForm.code"></el-input>
            </el-form-item>
            <div>
              <img :src="captchaImageUrl" alt="验证码" class="codeImg" />
            </div>
          </div>
          <el-button type="primary" class="login-button" @click="handleLogin"
            >登录</el-button
          >
          <el-button type="primary" class="register-button" @click="changePage"
            >注册</el-button
          >
        </el-form>
      </div>

      <div id="register_form" v-else>
        <el-form ref="formRef" :model="form" class="login-form" :rules="rules">
          <div style="display: flex">
            <img src="@/assets/logo.png" alt="Local Image" class="img" />
            <div class="title-container">
              <h2 class="title">欢迎登陆超市进销管理系统</h2>
            </div>
          </div>
          <el-form-item prop="account">
            <svg-icon icon="user" class="svg-container"></svg-icon>
            <el-input v-model="registerForm.account"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <svg-icon icon="password" class="svg-container"></svg-icon>
            <el-input
              v-model="registerForm.password"
              :type="passwordType"
            ></el-input>
            <svg-icon
              :icon="passwordType === 'password' ? 'eye' : 'eye-open'"
              @click="changeType"
            ></svg-icon>
          </el-form-item>
          <el-form-item prop="checkPassword">
            <svg-icon icon="password" class="svg-container"></svg-icon>
            <el-input
              v-model="registerForm.checkPassword"
              :type="passwordType"
            ></el-input>
            <svg-icon
              :icon="passwordType === 'password' ? 'eye' : 'eye-open'"
              @click="changeType"
            ></svg-icon>
          </el-form-item>
          <el-button type="primary" class="confirm" @click="handleRegister"
            >确认注册</el-button
          >
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import axios from 'axios'
import { ref } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
const store = useStore()
const loginForm = ref({
  account: 'admin',
  password: '123'
})
const registerForm = ref({
  account: 'admin',
  password: '123',
  checkPassword: '123'
})

const page = ref(true)

const changePage = () => {
  page.value = false
}

const rules = ref({
  account: [
    {
      required: true,
      message: '请输入账号',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ],
  code: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'blur'
    }
  ],
  checkPassword: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ]
})

const formRef = ref(null)

const handleLogin = () => {
  console.log(loginForm.value.code)
  if (
    loginForm.value.account &&
    loginForm.value.password &&
    loginForm.value.code
  ) {
    store.dispatch('app/login', loginForm.value)
  } else {
    ElMessage({
      type: 'error',
      message: '数据缺失，登录失败'
    })
  }
}

const handleRegister = async () => {
  const res = await axios.post(
    'http://127.0.0.1:8090/register',
    registerForm.value
  )
  if (res.data.code === 200) {
    ElMessage({
      type: 'success',
      message: '注册成功！！！'
    })
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg
    })
  }
  page.value = true
}

const passwordType = ref('password')

const captchaImageUrl = 'http://127.0.0.1:8090/getVerifiCodeImage?'

const changeType = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}
</script>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/background.png') no-repeat;
  background-size: cover;
  overflow: hidden;

  .img {
    width: 70px;
    height: 70px;
    margin-bottom: 30px;
  }

  .form {
    margin-left: -32%;
    margin-top: 60px;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    ::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
    .login-button {
      box-sizing: border-box;
      width: 40%;
    }
    .register-button {
      box-sizing: border-box;
      margin-left: 70px;
      width: 40%;
    }
    .confirm {
      width: 60%;
      box-sizing: border-box;
      margin-left: 18%;
    }
  }

  .tips {
    font-size: 16px;
    line-height: 28px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: black;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      margin-left: 38px;
      margin-top: 20px;
    }

    ::v-deep .lang-select {
      position: absolute;
      top: 4px;
      right: 0;
      background-color: white;
      font-size: 22px;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .show-pwd {
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .code {
    display: flex;
  }
  .codeImg {
    width: 100px;
    height: 50px;
    margin-left: 30px;
  }
}
</style>
